package top.shiyiri.exer;

import org.junit.Test;

/**
 * @author Aunean
 * @date 2022/1/1
 */
public class Exercise {

    @Test
    public void test1() {
        //模拟一个trim方法，去除字符串两端的空格。
        String s1 = " dfs df ";
        String trim = s1.trim();
        System.out.println("-"+trim+"-");
        String s = MyTrim(s1);
        System.out.println("-"+s1+"-");
    }

    public String MyTrim(String s1) {
        String result = "";
        String begin = s1.substring(0, 1);
        String end = s1.substring(s1.length()-1);
        if (begin.equals(" ")) {
//            result = s1.substring()
        }
        if (end.equals(" ")) {
            System.out.println("dddd");
        }
        char[] chars = s1.toCharArray();
        int len = s1.length()-1;
        return result;
    }

    @Test
    public void Test2() {
        //将一个字符串进行反转。将字符串中指定部分进行反转。比如“abcdefg”反
        //转为”abfedcg”
        String s1 = "abcdefg";
        String result = reverse(s1, 2, 6);
        System.out.println(result);

    }

    public String reverse(String s1, int startIndex, int endIndex) {
        //方式一
/*        String result = "";
        String s2 = s1.substring(0, startIndex);
        result += s2;
        String ss = s1.substring(startIndex, endIndex);
        char[] chars = ss.toCharArray();
        String s3 = s1.substring(endIndex);
        for (int i = chars.length-1; i >= 0; i--) {
            result += chars[i];
        }
        result += s3;
        return result;*/

        //方式二
/*        if (s1 != null && s1.length() != 0) {
            char[] arr = s1.toCharArray();
            for (int x = startIndex, y = endIndex-1; x < y; x++, y--) {
                char temp = arr[x];
                arr[x] = arr[y];
                arr[y] = temp;
            }
            return new String(arr);
        }
        return null;*/

        //方式三
        StringBuilder stringBuilder = new StringBuilder(s1.length());
        //第一部分
        stringBuilder.append(s1, 0, startIndex);
        //第二部分
        for (int i = endIndex-1; i >= startIndex; i --) {
            stringBuilder.append(s1.charAt(i));
        }
        //第三部分
        stringBuilder.append(s1, endIndex, endIndex+1);
        return new String(stringBuilder);
    }

    @Test
    public void test3() {
        //获取一个字符串在另一个字符串中出现的次数。
        //比如：获取“ ab”在 “abkkcadkabkebfkabkskab” 中出现的次数
        String s1 = "abkkcadkabkebfkabkskab";
        //方式一
        int count = 0;
        for (int i = 0; i < s1.length()-1; i++) {
            if (s1.substring(i, i+2).equals("ab")) {
                count ++;
            }
        }
        System.out.println(count);

        //方式二
        String subStr = "ab";
        int subStrLen = subStr.length();
        int index = 0;
        int count2 = 0;
        if (s1.length() > subStrLen) {
            while ((index = s1.indexOf(subStr, index)) != -1) {
                count2 ++;
                index = index + subStrLen;
            }
        } else {
            System.out.println(0);
        }
        System.out.println(count2);
    }

    @Test
    public void test4() {
        //获取两个字符串中最大相同子串。比如：
        //str1 = "abcwerthelloyuiodef“;str2 = "cvhellobnm"
        //提示：将短的那个串进行长度依次递减的子串与较长的串比较
        String str1 = "abcwerthello12yuiodefsdfwesdff";
        String str2 = "cvhellobnmcwerthello12";
        String contain = "";
        if (str1.length() > str2.length()) {
            for (int i = 0; i < str2.length(); i ++) {
                for (int j = i; j < str2.length(); j++) {
                    //System.out.println(str2.substring(i, j+1));
                    if (str1.contains(str2.substring(i, j+1))) {
                        String x = str2.substring(i, j+1);
                        if (x.length() > contain.length()) {
                            contain = x;
                        }
                    }
                }

            }
        } else {
            for (int i = 0; i < str1.length(); i ++) {
                if (str2.contains(str1.substring(i))) {
                    contain = str1.substring(i);
                    return;
                }
            }
        }
        System.out.println(contain);
    }

    public String getMaxSameString(String str1, String str2) {

        return null;
    }
}
